#!/usr/bin/env bash
# 本脚本用于部署PostgreSQL HA集群，工具为ansible
# 1、请确认是否部署有ansible工具
# 2、请在HOST1,HOST2,HOST3处填写ip，并确认ssh可达

# 默认为三节点集群，请填写服务器ip
HOST1=
HOST2=
HOST3=

# 判断执行用户是否为root
if [ $UID -ne 0 ];then
  echo "请使用root用户执行"
  exit
fi
# 创建工作目录
mkdir -p ~/DeployPgsql

# 判断当前机器是部署有ansible
CheckAnsibleStatus(){
  command -v ansible-playbook > /dev/null 2>&1 ||{ echo >&2 "请安装ansible，并且配置本机与pgsql所在服务器ssh互信";exit 1; }
}

PrepareAnsiblePlaybook(){
  cd ~/DeployPgsql
cat >host.ini<<EOF
[pgcluster]
$HOST1
$HOST2
$HOST3
EOF

cat >buildenv.yaml <<EOF
---
- hosts: pgcluster
  remote_user: root
  gather_facts: no
  tasks:
    - name: install gcc gcc-c++ make
      yum:
        name: "{{ item }}"
        state: present
      loop:
        - gcc
        - gcc-c++
        - make
    - name: update openssl
      yum:
        name: openssl
        state: latest
    - name: create user postgres
      user:
        name: postgres
        home: /home/postgres
        password: 1qaz)WSX
        state: present
EOF

}

CheckHOSTEnv(){
  for i in `awk /HOST/{print} host.ini`
    do
      if [ `echo $i | wc -c` -lt 8 ];then
        echo "${i:0:5} IP为空，请填入ip"
        exit 2
      else
        echo "${i:0:5} 的ip为 ${i:6}"
        ssh root@${i:6} -o StrictHostKeyChecking=no -o ConnectTimeout=1  "ls >/dev/null 2>&1"
        if [ $? -ne 0 ];then
          echo "ansible服务器与 ${i:0:5} 服务器未配置ssh互信，请配置ssh互信"
          exit 3
        fi
      fi
    done

}

BeforeBuildPgsql(){
  yum -y install gcc gcc-c++ make
  yum -y update openssl
  ansible-playbook -i host.ini buildenv.yaml
}

CompilerPostgreSQL(){
  # 获取源码
  cd ~/DeployPgsql
  curl -o postgresql-13.3.tar.gz https://ftp.postgresql.org/pub/source/v13.3/postgresql-13.3.tar.gz
  # 编译
  tar zvxf postgresql-13.3.tar.gz
  cd postgresql-13.3
  ./configure --prefix=/root/DeployPgsql/pgsql --with-perl --with-python --with-openssl
  make all
  make install
  chown -R postgres:postgres /root/DeployPgsql/pgsql
}

PrepareDeplyPlaybook(){
cd ~/DeployPgsql
cat >postgres.ini<<EOF
[postgresql]
$HOST1 ansible_port=22 ansible_user=postgres ansible_ssh_pass=1qaz)WSX
$HOST2 ansible_port=22 ansible_user=postgres ansible_ssh_pass=1qaz)WSX
$HOST3 ansible_port=22 ansible_user=postgres ansible_ssh_pass=1qaz)WSX
EOF

cat >pgsqlha.yaml<<EOF
---
- hosts: pgcluster
  remote_user: root
  gather_facts: no
  tasks:
    - name: rsync pgsql
      synchronize:
        src: /root/DeployPgsql/pgsql
        dest: /usr/local/
        owner: yes
        group: yes
EOF

}

# 分发pgsql二进制文件
DeployPostgresql(){

  ansible-playbook -v -i host.ini pgsqlha.yaml
}

# 初始化postgresql
InitPostgreSql(){
cd ~/DeployPgsql
cat>initdb.yaml<<EOF
---
- hosts: pgcluster
  remote_user: postgres
  gather_facts: no
  tasks:
    - name: init db
      shell: "source /etc/profile ; /usr/local/pgsql/bin/initdb -D /home/postgres/data"
EOF

ansible-playbook -v -i host.ini initdb.yaml
}

EditPgsqlConfig(){
cd ~/DeployPgsql
cat>ModifyPgsqlConfig.yaml<<EOF
---
- hosts: pgcluster
  remote_user: postgres
  gather_facts: no
  tasks:
    - name: Modify configuration pg_hba.conf
      shell: "/bin/echo {{ item }} >> /home/postgres/data/pg_hba.conf"
      with_items:
        - host    stack           devopser        0.0.0.0                 md5
        - host    stack_pro       devopser        0.0.0.0                 md5
    - name: Modify configuration postgresql.conf
      shell: sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /home/postgres/data/postgresql.conf
EOF

ansible-playbook -v -i postgres.ini ModifyPgsqlConfig.yaml
}

main(){
  CheckAnsibleStatus
  PrepareAnsiblePlaybook
  CheckHOSTEnv
  CompilerPostgreSQL
  PrepareDeplyPlaybook
  DeployPostgresql
  InitPostgreSql
  EditPgsqlConfig

}

main